/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Forms;

import BLL.BLLPhongKham;
import DTO.DTOPhongKham;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Documents
 */
public class PhongKham extends javax.swing.JFrame {
    BLLPhongKham bllPK = new BLLPhongKham();
    DefaultTableModel modelPK;
    
    /**
     * Creates new form PhongKham
     */
    public PhongKham() {
        initComponents();
        LayDSVaoBang();
    }

    private void LayDSVaoBang(){
        try {
            modelPK = new DefaultTableModel();
            String muc[] = {"Mã phòng", "Tên phòng"};
            modelPK.setColumnIdentifiers(muc);
            for(DTOPhongKham dtoPK : bllPK.layDSPhongKham()){
                modelPK.addRow(new Object[]{dtoPK.getMapk(), dtoPK.getTenpk()});
            }
            jTbDSPhongKham.setModel(modelPK);
        } 
        catch (Exception e) {
            System.err.print("Lấy dữ liệu không thành công");
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jpnlHoSo = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTbDSPhongKham = new javax.swing.JTable();
        btnReload = new javax.swing.JButton();
        jpnlTuyChon = new javax.swing.JPanel();
        jbtnTim = new javax.swing.JButton();
        jtxtTim = new javax.swing.JTextField();
        jbtnThem = new javax.swing.JButton();
        jbtnSua = new javax.swing.JButton();
        jbtnXoa = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 102, 0));
        jLabel1.setText("DANH SÁCH PHÒNG KHÁM");
        jLabel1.setToolTipText("");

        jpnlHoSo.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(null, new java.awt.Color(0, 0, 102)), "Danh sách phòng khám"));

        jTbDSPhongKham.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTbDSPhongKham);

        btnReload.setText("Tải lại");
        btnReload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReloadActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpnlHoSoLayout = new javax.swing.GroupLayout(jpnlHoSo);
        jpnlHoSo.setLayout(jpnlHoSoLayout);
        jpnlHoSoLayout.setHorizontalGroup(
            jpnlHoSoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnlHoSoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnlHoSoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 781, Short.MAX_VALUE)
                    .addGroup(jpnlHoSoLayout.createSequentialGroup()
                        .addComponent(btnReload)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jpnlHoSoLayout.setVerticalGroup(
            jpnlHoSoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnlHoSoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnReload)
                .addGap(5, 5, 5)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jpnlTuyChon.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(null, new java.awt.Color(0, 0, 102)), "Tùy chọn"));

        jbtnTim.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/IconsLandVista.png"))); // NOI18N
        jbtnTim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnTimActionPerformed(evt);
            }
        });

        jbtnThem.setText("Thêm");
        jbtnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnThemActionPerformed(evt);
            }
        });

        jbtnSua.setText("Sửa");
        jbtnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSuaActionPerformed(evt);
            }
        });

        jbtnXoa.setText("Xóa");
        jbtnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnXoaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpnlTuyChonLayout = new javax.swing.GroupLayout(jpnlTuyChon);
        jpnlTuyChon.setLayout(jpnlTuyChonLayout);
        jpnlTuyChonLayout.setHorizontalGroup(
            jpnlTuyChonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpnlTuyChonLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jbtnTim, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtxtTim, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbtnThem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbtnSua)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbtnXoa)
                .addGap(7, 7, 7))
        );
        jpnlTuyChonLayout.setVerticalGroup(
            jpnlTuyChonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnlTuyChonLayout.createSequentialGroup()
                .addGroup(jpnlTuyChonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtxtTim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jpnlTuyChonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jbtnTim)
                        .addComponent(jbtnThem)
                        .addComponent(jbtnSua)
                        .addComponent(jbtnXoa)))
                .addGap(0, 3, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(249, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(246, 246, 246))
            .addComponent(jpnlHoSo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jpnlTuyChon, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jpnlHoSo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpnlTuyChon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnThemActionPerformed
        ThemPhongKham form = new ThemPhongKham();
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        form.setLocation((dim.width - form.getWidth())/2, (dim.height - form.getHeight())/2);
        form.setVisible(true);
    }//GEN-LAST:event_jbtnThemActionPerformed

    private void jbtnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSuaActionPerformed
        try {
            int maPK = -1;
            maPK = Integer.valueOf(jTbDSPhongKham.getValueAt(jTbDSPhongKham.getSelectedRow(), 0).toString());
            if(maPK != -1){
                SuaPhongKham form = new SuaPhongKham(maPK);
                Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
                form.setLocation((dim.width - form.getWidth())/2, (dim.height - form.getHeight())/2);
                form.setVisible(true);
            }
            else
                JOptionPane.showMessageDialog(null, "Vui lòng phòng khám cần sửa");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Vui lòng chọn phòng khám cần sửa");
        }
        
    }//GEN-LAST:event_jbtnSuaActionPerformed

    private void jbtnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnXoaActionPerformed
        try {
            int[] dong = jTbDSPhongKham.getSelectedRows();
            if(dong.length > 0){
                if(JOptionPane.showConfirmDialog(null, "Bạn có chắc muốn xóa?", "Thông báo",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION){
                    int dem = 0;
                    for(int i = dong.length - 1; i >= 0; i--){
                        int maPK = Integer.valueOf(jTbDSPhongKham.getValueAt(dong[i], 0).toString());
                        bllPK.XoaPhongKham(maPK);
                        modelPK.removeRow(dong[i]);
                        dem++;
                    }
                    if(dem==dong.length){
                        JOptionPane.showMessageDialog(null, "Đã xóa các bệnh nhân thành công");
                    }
                }
            }
            else
                JOptionPane.showMessageDialog(null, "Vui lòng chọn mục muốn xóa");
        } catch (Exception e) {
            
        }
        
        
    }//GEN-LAST:event_jbtnXoaActionPerformed

    private void jbtnTimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnTimActionPerformed
        try {
            for (int i = modelPK.getRowCount() -1; i >= 0; i--) {
                modelPK.removeRow(i);
            }
            String tim = jtxtTim.getText();
            ArrayList<DTOPhongKham> dsPhongKham = bllPK.TimPhongKham(tim);
            for(DTOPhongKham dtoPK : dsPhongKham){
                modelPK.addRow(new Object[]{dtoPK.getMapk(), dtoPK.getTenpk()});
            }
        } catch (Exception e) {
        }
    }//GEN-LAST:event_jbtnTimActionPerformed

    private void btnReloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReloadActionPerformed
       for(int i = modelPK.getRowCount() -1; i>=0; i--){
           modelPK.removeRow(i);
       }
       for(DTOPhongKham dtoPK : bllPK.layDSPhongKham()){
            modelPK.addRow(new Object[]{dtoPK.getMapk(), dtoPK.getTenpk()});
        }
    }//GEN-LAST:event_btnReloadActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PhongKham.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PhongKham.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PhongKham.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PhongKham.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PhongKham().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnReload;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTbDSPhongKham;
    private javax.swing.JButton jbtnSua;
    private javax.swing.JButton jbtnThem;
    private javax.swing.JButton jbtnTim;
    private javax.swing.JButton jbtnXoa;
    private javax.swing.JPanel jpnlHoSo;
    private javax.swing.JPanel jpnlTuyChon;
    private javax.swing.JTextField jtxtTim;
    // End of variables declaration//GEN-END:variables
}
